package com.xhj.xmly.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.Collection;
import java.util.List;

public class CommentAdapter<Data> extends BaseAdapter {
    private Context context;
    private List<Data> list;
    private int layoutId;
    private int variableId;

    public CommentAdapter(Context context, List<Data> list, int layoutId, int variableId) {
        this.context = context;
        this.list = list;
        this.layoutId = layoutId;
        this.variableId = variableId;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Data getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public void addAll(Collection<? extends Data> collection) {
        list.addAll(collection);
        notifyDataSetChanged();
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = DataBindingUtil.inflate(LayoutInflater.from(context), layoutId, viewGroup, false).getRoot();
        }
        ViewDataBinding binding = DataBindingUtil.bind(view);
        binding.setVariable(variableId, list.get(i));
        return view;
    }
}
